import React from 'react'
import {connect} from 'react-redux'
import { Switch, Spin, message } from 'antd';
import SearchArea from 'components/search-area';
import setOfBooksService from 'containers/finance-setting/set-of-books/set-of-books.service';
import {deepFullCopy, messages } from "share/common";
import ResizeTable from 'components/resize-table'

class writeOff extends React.Component{
  constructor(props){
    super(props);
    this.state = {
      setOfBooks: {},
      searchForm: [
        {type: 'input', id: 'ruleCode', label: messages('finance-setting.key71')/*规则编码*/},
        {type: 'select', id: 'differentCurrency', label: messages('finance-setting.key159')/*异币种核销*/,
        options: [
          {label: 'Y', value: true},
          {label: 'N', value: false}
        ]}
      ],
      searchParams:{},
      columns: [
        {title: messages('finance-setting.key71')/*规则编码*/, dataIndex: 'ruleCode'},
        {title: messages('finance-setting.key160')/*核销币种*/, dataIndex: 'paymentScheduleCurrencyName'},
        {title: messages('finance-setting.key161')/*被核销币种*/, dataIndex: 'loanLineCurrencyName'},
        {title: messages('finance-setting.key159')/*异币种核销*/, dataIndex: 'differentCurrency', render: value => value ? 'Y' : 'N'},
        {title: messages('finance-setting.key162')/*核销汇率*/, dataIndex: 'paymentScheduleRateName'},
        {title: messages('finance-setting.key163')/*被核销汇率*/, dataIndex: 'loanLineRateName'},
        {title: messages('common.column.status')/*状态*/, dataIndex: 'enable',width: '15%', render: (value,record) => {
          return (
            <div>
              <Switch defaultChecked={value} onChange={checked => this.handleSave(checked,record)} disabled={!props.tenantMode}/>
              <div style={{textAlign: 'right'}}>
                {value ? messages('common.status.enable')/*启用*/ : messages('common.status.disable')/*禁用*/}
              </div>
            </div>
          )}}
      ],
      data: [],
      loading: false,
      pagination: {
        total: 0
      },
      page: 0,
      saveLoading: false
    }
  }

  componentWillMount() {
    this.setState({setOfBooks: this.props.setOfBooks},() => {
      this.getList();
    });
  }

  componentWillReceiveProps(nextProps) {
    this.setState({setOfBooks: nextProps.setOfBooks});
  }

  getList = () => {
    const { setOfBooks, searchParams, page } = this.state;
    let temp = deepFullCopy(searchParams);
    temp.page = page;
    temp.setOfBooksId = setOfBooks.setOfBooksId;
    this.setState({loading: false});
    setOfBooksService.getWriteOffList(temp).then(res => {
      this.setState({
        loading: false,
        data: res.data,
        pagination: {
          total: Number(res.headers['x-total-count']),
          current: page + 1,
          onChange: this.onChangePager,
        }
      });
    }).catch(e => {
      message.error(e.response.data.message);
      this.setState({loading: false});
    });
  };

  onChangePager = (page) => {
    if (page - 1 !== this.state.page)
      this.setState({
        page: page - 1
      }, () => {
        this.getList();
      })
  };

  search = (value) => {
    const { searchParams } = this.state;
    value.ruleCode && (searchParams.ruleCode = value.ruleCode);
    value.differentCurrency && (searchParams.differentCurrency = value.differentCurrency === 'Y');
    this.setState({searchParams}, () => {
      this.getList();
    })
  };

  clear = () => {
    this.setState({searchParams: {}},() => {
      this.getList();
    })
  };

  handleSave = (checked,record) => {
    let params = {
      id: record.id,
      enable: checked
    };
    this.setState({saveLoading: true});
    setOfBooksService.saveWriteOff(params).then(() => {
      this.setState({saveLoading: false},() => {
        message.success(messages('common.operate.success')/*操作成功*/);
        this.getList();
      });
    }).catch(e => {
      this.setState({saveLoading: false});
      message.error(e.response.data.message);
    })
  };

  render(){
    const { searchForm, columns, data, loading, pagination, saveLoading } = this.state;
    return(
      <Spin spinning={saveLoading}>
        <SearchArea searchForm={searchForm}
                    clearHandle={this.clear}
                    submitHandle={this.search}/>
        <ResizeTable
          loading={loading}
          style={{marginTop: '20px'}}
          pagination={pagination}
          bordered
          rowKey={'id'}
          dataSource={data}
          columns={columns}
        />
      </Spin>
    )
  }
}

function mapStateToProps(state){
  return {
    tenantMode: state.main.tenantMode
  }
}

export default connect(mapStateToProps)(writeOff);
